package com.software.content_server;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;



import java.util.ArrayList;
import java.util.List;


/**
 *  应用程序启动时，会加载
 *  创建或打开数据库
 */

public class UserDBHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "user.db";

    private static final int DB_VERSION = 1;

    public static final String TABLE_NAME = "user_info";
    //  类的静态属性，只会加载一次
    private static UserDBHelper userDBHelper;;
    private SQLiteDatabase read, write;

    private UserDBHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }
    //单例模式，懒汉模式 -> 饿汉模式
    //保证应用中有且只有一个实例，只有UserDBHelper，只有它才能操作user.db数据库
    public static UserDBHelper getInstance(Context context) {
        //如果是空的，则创建对象，否则直接返回
        if (userDBHelper == null) {
            userDBHelper = new UserDBHelper(context);
        }
        return userDBHelper;
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( " +
                " id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, " +
                " name VARCHAR NOT NULL, " +
                " age INT NOT NULL, " +
                " height INT NOT NULL, " +
                " weight FLOAT NOT NULL );";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }


}
